package com.ph.elec.gwt.util;

import java.util.List;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.TextBoxBase.TextAlignConstant;

public class UIUtils {
	
	public static Widget decorateFieldName(Widget field, String fieldName, boolean required) {
		HorizontalPanel hzpField = new HorizontalPanel();
		Label lblFieldName = new Label(fieldName);
		lblFieldName.addStyleName("fname");
		hzpField.add(lblFieldName);

		if (required) {
			Label lblRequired = new Label("*");
			lblRequired.addStyleName("req");
			hzpField.add(lblRequired);
		}
		
		VerticalPanel vtpRoot = new VerticalPanel();
		vtpRoot.add(hzpField);
		
		vtpRoot.add(field);
		vtpRoot.addStyleName("FieldCell");
		return vtpRoot;
	}
	
	public static Widget decorateFieldName(Widget field, String fieldName) {
		return decorateFieldName(field, fieldName, false);
	}

	public static TextBox decorateTextBox(TextBox field, String width, int maxLength) {
		return decorateTextBox(field, width, maxLength, TextBox.ALIGN_LEFT);
	}
	
	public static TextBox decorateTextBox(TextBox field, String width, 
			int maxLength, TextAlignConstant textAlign) {
		if (maxLength > 0) {
			field.setMaxLength(maxLength);
			field.setVisibleLength(maxLength);
		}
		field.setTextAlignment(textAlign);
		field.setWidth(width);
		return field;
	}

	public static TextBox decorateTextBox(TextBox field, String width) {
		return decorateTextBox(field, width, -1);
	}
	
	public static ListBox decorateListBox(ListBox listbox, List<String> items) {
		listbox.addItem("- select -"); // Add the top most item
		if (items != null && items.size() > 0) {
			for (String item : items) {
				listbox.addItem(item);
			}
			listbox.setSelectedIndex(0);
		}
		listbox.addStyleName("Listbox");
		return listbox;
	}

	public static ListBox decorateListBox(ListBox listbox) {
		return decorateListBox(listbox, null);
	}

	public static Widget decorateWidth(Widget field, String width) {
		field.setWidth(width);
		return field;
	}

	public static Panel decorateBox(Panel panel, String title) {
		if (title != null && !title.isEmpty()) {
			VerticalPanel titlePanel = new VerticalPanel();
			Label lblTitle = new Label(title);
			lblTitle.addStyleName("PnlTitle");
			titlePanel.add(lblTitle);
			titlePanel.add(panel);
			panel = titlePanel;
		}
		panel.addStyleName("Panel");
		return panel;
	}

	public static Panel decorateBox(Panel panel) {
		return decorateBox(panel, null);
	}
	
	public static Panel buttonArea(List<Widget> widgets) {
		HorizontalPanel pnlButton = new HorizontalPanel();
		for (Widget widget : widgets) {
			pnlButton.add(widget);			
		}
		pnlButton.setStyleName("ButtonArea");
		pnlButton.setWidth("500px");
		return pnlButton;
	}

	public static Button decorateButton(Button button, String buttonText) {
		if (buttonText != null && !buttonText.isEmpty()) {
			button.setText(buttonText);			
		}
		button.addStyleName("Button");
		return button;
	}

	public static Widget decorateRichTextArea(RichTextArea richText,
			String width) {
		richText.setWidth(width);
		return richText;
	}

	public static Widget decorateTextArea(TextArea txaText, String width, int rowHeight) {
		txaText.setWidth(width);
		txaText.setHeight((rowHeight * 14) + "px");
		return txaText;
	}

	public static void popAutoHideDialog(String title, String content) {
		DialogBox box = new DialogBox(true, true);
		box.setTitle(title);
		box.setText(content);
		box.center();
	}

	public static class StdDecoratorPanel extends DecoratorPanel {
		
		protected VerticalPanel vtpRoot = new VerticalPanel();
		protected VerticalPanel vtpErrorMessage = new VerticalPanel(); 
		
		public StdDecoratorPanel() {
			vtpErrorMessage.setWidth("100%");
			vtpErrorMessage.addStyleName("errorPnl");
			vtpErrorMessage.setVisible(false);
			vtpRoot.add(vtpErrorMessage);
		}
		
		public void addErrorMessage(String message) {
			if (!(message == null || message.length() == 0)) {
				vtpErrorMessage.add(new Label(message));
				vtpErrorMessage.setVisible(true);
			}
		}

		public void addMultipleErrorMessage(String message) {
			vtpErrorMessage.clear();
			for (String message_piece : message.split("\n")) {
				addErrorMessage(message_piece);
			}
		}
	}

}
